//
//  RegisterViewController.swift
//  YueJian
//
//  Created by Milo.o on 2017/11/2.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire

class RegisterViewController: UIViewController {
    var password :UITextField?
    var account:UITextField?
    let Swidth = UIScreen.main.bounds.width
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = UIImageView(image: UIImage(named: "c"))
        view.isUserInteractionEnabled = true
        self.view.backgroundColor = UIColor.white
        account = UITextField(frame: CGRect(x: 30, y: 150, width: Swidth-60, height: 50))
        account?.placeholder = "账号"
        account?.borderStyle = .line
        self.view.addSubview(account!)
        
        password = UITextField(frame: CGRect(x: 30, y: 220, width: Swidth-60, height: 50))
        password?.placeholder = "密码"
        password?.borderStyle = .line
        password?.isSecureTextEntry = true
        self.view.addSubview(password!)
        
        let registerBtn = UIButton(frame: CGRect(x: 30, y: 360, width: Swidth-60, height: 50))
        registerBtn.setTitleColor(UIColor.white, for: .normal)
        registerBtn.setTitle("注册", for: UIControlState.normal)
        registerBtn.addTarget(self, action: #selector(register), for: UIControlEvents.touchUpInside)
        registerBtn.backgroundColor = UIColor.black
        self.view.addSubview(registerBtn)

        
        let exitBtn = UIButton(frame: CGRect(x: 30, y: 420, width: Swidth-60, height: 50))
        exitBtn.setTitleColor(UIColor.white, for: .normal)
        exitBtn.setTitle("取消", for: UIControlState.normal)
        exitBtn.addTarget(self, action: #selector(exit), for: UIControlEvents.touchUpInside)
        exitBtn.backgroundColor = UIColor.black
        self.view.addSubview(exitBtn)
        // Do any additional setup after loading the view.
    }

    @objc func exit() {
        self.dismiss(animated: true) {
            
        };
    }
    
    @objc func register() {
        Alamofire.request(URL(string: "http://172.24.10.175/workout/api.php/reg")!, method: .get, parameters: ["tel":account?.text ?? 0,"password":password?.text ?? 3], encoding: URLEncoding.default, headers: nil).responseJSON { (responcse) in
            
            print(responcse)
            let dic = responcse.result.value as! NSDictionary
            let keys = dic.allKeys as! NSArray
            if keys.contains("error") {
                print("错误")
            } else {
                self.dismiss(animated: true, completion: {
                    
                });
                
            }
            
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
